package com.fxkj.cloud.drms.ui.listener;

import com.fxkj.cloud.dbs.common.event.stream.RemoteAlarmAttributeCreateEvent;
import com.fxkj.cloud.dbs.common.event.stream.RemoteAlarmAttributeDeleteEvent;
import com.fxkj.cloud.dbs.common.model.attribute.AttributeConstants;
import com.fxkj.cloud.drms.application.ProductApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/7 13:04
 */
@Component
@Slf4j
public class RemoteAlarmAttributeDeleteListener {

    private final ProductApplicationService productApplicationService;

    public RemoteAlarmAttributeDeleteListener(ProductApplicationService productApplicationService) {
        this.productApplicationService = productApplicationService;
    }

    @Bean
    public Consumer<RemoteAlarmAttributeDeleteEvent> alarmAttributeDelete() {
        return event -> {
            log.trace("收到远程产品报警设置删除事件:{}", event);
            List<Integer> sns = event.getSns();
            if (sns == null || sns.isEmpty()) {
                return;
            }
            Set<String> alarmKeys = new HashSet<>();
            sns.stream().forEach(sn -> {
                alarmKeys.add(AttributeConstants.Service.W.Code.S.getFullCode() + sn.toString());
                alarmKeys.add(AttributeConstants.Service.W.Code.T.getFullCode() + sn.toString());
                alarmKeys.add(AttributeConstants.Service.W.Code.E.getFullCode() + sn.toString());
                alarmKeys.add(AttributeConstants.Service.W.Code.SN.getFullCode() + sn.toString());
                alarmKeys.add(AttributeConstants.Service.W.Code.V.getFullCode() + sn.toString());
            });
            productApplicationService.removeProductAttribute(event.getProductId(), alarmKeys);
        };
    }
}
